package com.kainitram.library.sm;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;

import com.actionbarsherlock.view.MenuItem;
import com.kainitram.library.R;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

/**
 * Basic class for sliding activity
 * 
 * @author Mino develop
 * 
 */
public abstract class BaseSlidingFragmentActivity extends
		SlidingFragmentActivity {

	private static final String CONTENT_TAG = "mContent";
	protected SlidingContentFragment mContent;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (savedInstanceState != null) {
			mContent = (SlidingContentFragment) getSupportFragmentManager()
					.getFragment(savedInstanceState, CONTENT_TAG);
		}
		if (mContent == null) {
			mContent = getDefaultContentFragment();
		}

		setContentView(getSlidingActivityViewID());
		getSupportFragmentManager().beginTransaction()
				.replace(getContentFrameID(), (Fragment) mContent)
				.commit();

		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		setSlidingActionBarEnabled(false);
	}

	/**
	 * switch content to concrete fragment
	 * 
	 * @param fragment
	 *            to be displayed
	 */
	public void switchContent(SlidingContentFragment fragment) {
		mContent = fragment;
		getSupportFragmentManager().beginTransaction()
				.replace(getContentFrameID(), (Fragment) fragment).commit();
					
		Handler h = new Handler();
		h.postDelayed(new Runnable() {
			public void run() {
				getSlidingMenu().showContent();
			}
		}, 50);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			toggle();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		getSupportFragmentManager().putFragment(outState, CONTENT_TAG,
				(Fragment) mContent);
	}

	// ------------------------------------
	// --------- abstract methods ---------
	// ------------------------------------

	/**
	 * Get application title for displaying in action bar
	 * 
	 * @return application title
	 */
	public abstract String getApplicationTitle();

	/**
	 * Get view id for content
	 * 
	 * @return content view id.
	 */
	protected abstract int getContentFrameID();
	
	
	/**
	 * Get view id for activity
	 * @return activity view id.
	 */
	protected abstract int getSlidingActivityViewID();
	

	/**
	 * Get default content fragment for this activity.
	 * 
	 * @return default fragment for this activity.
	 */
	protected abstract SlidingContentFragment getDefaultContentFragment();

}
